//
//  LBTabBarController.m
//  LvoeBeen_Imitation
//
//  Created by Mac on 17/2/18.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "LBTabBarController.h"
#import "LBTabBar.h"
#import "LBShoppingTrollyViewController.h"
#import "LBScanViewController.h"

@interface LBTabBarController () <LBTabBarDelegate>

@end

@implementation LBTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    LBTabBar *tabBar = [[LBTabBar alloc] initWithFrame:CGRectMake(0, ScreenHeight - TabBarHeight, ScreenWidth, TabBarHeight)];
    tabBar.showShoppingTrollyDelegate = self;
    [self setValue:tabBar forKey:@"tabBar"];
    
    [self buildTabBarControllerUI];
}

// 搭建 UI 界面
- (void) buildTabBarControllerUI {
    LBNavigationController *homeNavi = [self creatChildControllerWithRootViewController:@"LBHomeViewController" titleName:@"首页" imageName:@"v2_home"];
    LBNavigationController *lightNavi = [self creatChildControllerWithRootViewController:@"LBLightningViewController" titleName:@"闪电超市" imageName:@"v2_order"];
    LBNavigationController *profileNavi = [self creatChildControllerWithRootViewController:@"LBProfileViewController" titleName:@"我的锅" imageName:@"v2_my"];
    
    [self addChildViewController:homeNavi];
    [self addChildViewController:lightNavi];
    [self addChildViewController:profileNavi];
}

// 创建子控制器
- (LBNavigationController *) creatChildControllerWithRootViewController:(NSString *)viewControllerName titleName:(NSString *)titleName imageName:(NSString *)imageName{
    
    Class cls = NSClassFromString(viewControllerName);
    LBBaseViewController *vc = [cls new];
    
    vc.tabBarItem.title = titleName;
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:(UIControlStateNormal)];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor]} forState:(UIControlStateSelected)];
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_r",imageName]] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    LBNavigationController *navi = [[LBNavigationController alloc] initWithRootViewController:vc];
    
    if ([viewControllerName isEqualToString:@"LBHomeViewController"] || [viewControllerName isEqualToString:@"LBLightningViewController"]) {
        [self buildNavigationBarUI:vc];
    }
    
    return navi;
}

- (void)showShoppingTrolly:(LBTabBar *)tabBar {
    LBShoppingTrollyViewController *shoppingTrolly = [LBShoppingTrollyViewController new];
    
    LBNavigationController *navi = self.selectedViewController;
    [navi pushViewController:shoppingTrolly animated:YES];
}

#pragma mark
#pragma mark - 为 VC 添加左右 导航栏元素
- (void) buildNavigationBarUI:(UIViewController *)viewController {
    // 右端项目
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, navigationBarHeight)];
    [rightButton setImage:[UIImage imageNamed:@"icon_search"] forState:(UIControlStateNormal)];
    [rightButton setTitle:@"搜索" forState:(UIControlStateNormal)];
    [rightButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [rightButton setImageEdgeInsets:(UIEdgeInsetsMake(1, 10.25, 13, -10.25))];
    [rightButton setTitleEdgeInsets:(UIEdgeInsetsMake(30, -11.85, 1, 11.85))];
    [rightButton addTarget:self action:@selector(clickRightBarButtonItem) forControlEvents:(UIControlEventTouchUpInside)];
    viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    // 左端项目
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, navigationBarHeight)];
    [leftButton setImage:[UIImage imageNamed:@"icon_black_scancode"] forState:(UIControlStateNormal)];
    [leftButton setTitle:@"扫一扫" forState:(UIControlStateNormal)];
    [leftButton addTarget:self action:@selector(clickLeftBarButtonItem) forControlEvents:(UIControlEventTouchUpInside)];
    [leftButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:10];
    [leftButton setImageEdgeInsets:(UIEdgeInsetsMake(1, 6, 13, -6))];
    [leftButton setTitleEdgeInsets:(UIEdgeInsetsMake(15, -20, -15, 0))];
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    // 中间 title
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    UILabel *middleLabel = [[UILabel alloc] init];
    middleLabel.text = @"人民大会堂";
    middleLabel.font = [UIFont systemFontOfSize:17];
    middleLabel.textColor = [UIColor blackColor];
    [middleLabel sizeToFit];
    
    UILabel *dispatchLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (middleLabel.bounds.size.height - 20) / 2, 50, 20)];
    dispatchLabel.text = @"配送至";
    dispatchLabel.textColor = [UIColor blackColor];
    dispatchLabel.font = [UIFont systemFontOfSize:12];
    middleLabel.center = CGPointMake(dispatchLabel.bounds.size.width + middleLabel.bounds.size.width / 2, middleLabel.bounds.size.height / 2);
    
    UIButton *arrowButton = [[UIButton alloc] init];
    [arrowButton setImage:[UIImage imageNamed:@"allowBlack"] forState:(UIControlStateNormal)];
    [arrowButton sizeToFit];
    arrowButton.center = CGPointMake(CGRectGetMaxX(middleLabel.frame) + arrowButton.bounds.size.width / 2 + 5, arrowButton.frame.size.height / 2);
    
    [view addSubview:middleLabel];
    [view addSubview:arrowButton];
    [view addSubview:dispatchLabel];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(middleLabel);
        make.right.equalTo(arrowButton);
        make.left.equalTo(dispatchLabel);
    }];
    
    viewController.navigationItem.titleView = view;
}

- (void) clickRightBarButtonItem {
    NSLog(@"%s",__func__);
}

- (void) clickLeftBarButtonItem {
    UINavigationController *navi = self.selectedViewController;
    LBScanViewController *scanVC = [LBScanViewController new];
    [navi pushViewController:scanVC animated:YES];
}

@end
